﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace 医院药库管理系统
{
    public partial class 采购申请 : Form
    {
        public 采购申请()
        {
            InitializeComponent();
        }

        private void bt_sqfh_Click(object sender, EventArgs e)
        {
            首页 f3 = new 首页();
            f3.Show();
        }

        private void 采购申请_Load(object sender, EventArgs e)
        {
           
            this.tb_DosageFormTableAdapter.Fill(this.yyaoDataSet6.tb_DosageForm);
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText = "SELECT * FROM tb_Report;";
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            sqlDataAdapter.SelectCommand = sqlCommand;
            DataTable csTable = new DataTable();
            sqlConnection.Open();
            sqlDataAdapter.Fill(csTable);
            sqlConnection.Close();
            this.dgv_cgsq.DataSource = csTable;
            this.dgv_cgsq.Columns["No"].HeaderText = "编号";
            this.dgv_cgsq.Columns["Name"].HeaderText = "名称";
            this.dgv_cgsq.Columns["ReportDate"].HeaderText = "申请日期";
            this.dgv_cgsq.Columns["FirmName"].HeaderText = "厂商名称";
            this.dgv_cgsq.Columns["DosageForm"].HeaderText = "剂型";
            this.dgv_cgsq.Columns["Principal"].HeaderText = "负责人";
            this.dgv_cgsq.Columns["Quantity"].HeaderText = "数量";
            this.dgv_cgsq.Columns["zt"].HeaderText = "申请状态";
            

        }

        private void bt_sqtj_Click(object sender, EventArgs e)
        {
            if (tb_sqbh.Text == "" || tb_sqmc.Text == "" || tb_sqcs.Text == "" || cb_cgjx.Text == "" ||  tb_fzr.Text == "")
            {
                MessageBox.Show("申请信息不能为空！");
            }
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText =
               "INSERT tb_Report (No,Name,ReportDate,FirmName,Quantity,DosageForm,Principal,zt) VALUES(@No,@Name,@ReportDate,@FirmName,@Quantity,@DosageForm,@Principal,'待处理');";
            sqlCommand.Parameters.AddWithValue("@No", this.tb_sqbh.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@Name", this.tb_sqmc.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@ReportDate", DateTime.Now);
            sqlCommand.Parameters.AddWithValue("@FirmName", this.tb_sqcs.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@Quantity", this.tb_sl.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@DosageForm", this.cb_cgjx.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@Principal", this.tb_fzr.Text.Trim());
            sqlConnection.Open();
            int rowAffected = sqlCommand.ExecuteNonQuery();
            sqlConnection.Close();
            if (rowAffected == 1)
            {
                MessageBox.Show("申请成功。");
            }
            else
            {
                MessageBox.Show("申请失败，请重试！");

            }
            sqlCommand.CommandText = "SELECT * FROM tb_Report;";
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            sqlDataAdapter.SelectCommand = sqlCommand;
            DataTable csTable = new DataTable();
            sqlConnection.Open();
            sqlDataAdapter.Fill(csTable);
            sqlConnection.Close();
            this.dgv_cgsq.DataSource = csTable; 
        }
    }
}
